#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import sys,re,urllib2,urllib,time,xlwt,os,json,codecs,csv,smtplib

sys_type = sys.getfilesystemencoding() 
reload(sys)  
sys.setdefaultencoding('utf8')
#糗事百科爬虫类
class Kindauto:        
    #开始方法
    def __init__(self):
        self.pageIndex  = 13
        self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        self.headers    = { 'User-Agent' : self.user_agent }
        self.month_day  = "18/04"
        self.stories    = []
        self.enable     = 1

    def getHtmlItems(self,pageHtml):
        # 捉取內容
        liContent   = re.findall('<li>\n.*<a href="(.*?)".*?>.*?></a>\n.*<div class="explain ">\n.*<span>(.*?)</span>\n.*<strong>(.*?)</strong>.*>\n.*">(.*?)</a>',pageHtml)

        # 循環輸入內容
        self.storeDate(liContent)

    def storeDate(self,liContent):
        for items in liContent:
            if( items[1][2:7] == self.month_day):
                self.stories.append(items)
                self.enable = 2
                print u"第%s頁,時間：%s"%(self.pageIndex,items[1][2:7])

            elif(self.enable == 2):
                self.enable = 3


        if(self.enable == 1 or self.enable==2):
            self.pageIndex += 1
            self.start()
        else: 
            print u"數據捉取完畢，一共%s"%len(self.stories)
            self.ExcelData()

    def ExcelData(self):
        if (self.stories):
            print u'./kindauto-共%s篇.csv'%(len(self.stories))
            # 写入数据
            for item in self.stories:
                if os.path.exists(u'kindauto-共%s篇.csv'%(len(self.stories))) == False:
                    with codecs.open(u'kindauto-共%s篇.csv'%(len(self.stories)), 'w', 'utf_8_sig') as f:
                        f.write('时间,觀看數,標題,鏈接')
                with codecs.open(u'kindauto-共%s篇.csv'%(len(self.stories)), 'a', 'utf_8') as f:
                    f.write(u'\r\n%s,%s,%s,%s'%(item[1],item[2].replace(",", ""),item[3],item[0]))
        else:
            print u'未找到数据'

    def start(self):
        url        = 'https://www.kingautos.net/all/page%s'%self.pageIndex
        request    = urllib2.Request(url, headers = self.headers)
        response   = urllib2.urlopen(request).read()
        pageHtml = response

        self.getHtmlItems(pageHtml)

spider = Kindauto()
spider.start()



